package baiduliu;

import java.util.*;

public class liuhanxue {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        HashMap<Integer, Deque> hashMap = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            Deque<Integer> deque = new ArrayDeque<>();
            deque.addLast(i);
            hashMap.put(i,deque);
        }
        for (int i = 0; i < m; i++) {
            String str = scan.next();
            int a = scan.nextInt();
            int b = scan.nextInt();

            if(str.equals("C")){
                Deque deque = hashMap.get(b);
                Deque deque1 = hashMap.get(a);
                while(!deque1.isEmpty()){
                    deque.addLast(deque1.removeFirst());
                }
            }
            if(str.equals("Q")){
                for(Map.Entry<Integer,Deque> de:hashMap.entrySet()){
                    Deque<Integer> value = de.getValue();
                    if(value.contains(a)){
                        if(value.contains(b)){
                            int temp = 0;
                            boolean flag = false;
                            Deque<Integer> deq = new ArrayDeque<>();
                            while(!value.isEmpty()){
                                if(value.peekFirst()==a){
                                    flag = true;
                                }
                                if(value.peekFirst()==b){
                                    flag = false;
                                }
                                if(flag){
                                    temp++;
                                }

                                deq.addLast(value.removeFirst());
                            }
                            while(!deq.isEmpty()){
                                value.addLast(deq.removeFirst());
                            }
                            System.out.println(temp-1);
                            break;
                        }else{
                            System.out.println(-1);
                            break;
                        }
                    }
                }
            }
        }
        /**
6 6
C 3 6
C 4 1
Q 1 6
C 1 6
Q 1 6
Q 2 5
         */
    }
    public void tan(int [] arr){
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]<0){
                continue;
            }else if(i==0) {
                if(arr[i]!=1){
                    System.out.println(1);
                    break;
                }
            }else {
                if(arr[i]!=arr[i-1]+1){
                    System.out.println(arr[i - 1] + 1);
                    break;
                }
            }
        }
    }
}
